package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.ApArticleInfoService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.article.dtos.CollectionBehaviorDto;
import com.heima.model.behavior.dto.LikesBehaviorDto;
import com.heima.model.behavior.dto.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.common.ApuserThreadLocalUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.util.HashMap;

@Service
public class ApArticleInfoServiceImpl implements ApArticleInfoService {
    @Autowired
    private CacheService cacheService;

    /**
     * 加载文章行为 数据回显
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        //1.参数检查
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.从线程中去获取用户信息   获取当前用户登录的id
        ApUser apUser = ApuserThreadLocalUtil.get();
        if (apUser ==null ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //3.获取用户的id
        Integer apUserId = null;
        if (apUser != null){
            apUserId = apUser.getId();
            if (apUserId == 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
            }
        }
        //定义返回值参数
        boolean isUnlike = false,isLike = false,isCollection = false,isFollow=false;
        //是否关注
        String followKey = "FOLLOW_" + apUserId;

        String follow = cacheService.hGet(followKey, dto.getArticleId() + "").toString();
        if (StringUtils.isNotEmpty(follow)){
            UserRelationDto userRelationDto = JSON.parseObject(follow, UserRelationDto.class);
            if (BehaviorConstants.FOLLOW.equals(userRelationDto.getOperation())){
                isFollow = true;
            }
        }



        //是否点赞
        String likeKey = "LIKE_" + apUserId;
        String likeString = cacheService.hGet(likeKey, dto.getArticleId() + "").toString();
        if (StringUtils.isNotEmpty(likeString)){
            LikesBehaviorDto likesBehaviorDto = JSON.parseObject(likeString, LikesBehaviorDto.class);
            if (BehaviorConstants.LIKE.equals(likesBehaviorDto.getOperation())){
                isLike = true;
            }
        }


        //是否不喜欢
        String unLikeKey = "UNLIKE_" + apUserId;
        String unLikeString = cacheService.hGet(unLikeKey, dto.getArticleId() + "").toString();
        if (StringUtils.isNotEmpty(unLikeString)){
            UnLikesBehaviorDto unLikesBehaviorDto = JSON.parseObject(unLikeString, UnLikesBehaviorDto.class);
            if (BehaviorConstants.UNLIKE.equals(unLikesBehaviorDto.getType())){
                isUnlike = true;
            }
        }


        //是否收藏
        String collectionKey = "COLLECTION_" + apUserId;
        String collectionString = cacheService.hGet(collectionKey, dto.getArticleId() + "").toString();
        if (StringUtils.isNotEmpty(collectionString)){
            CollectionBehaviorDto collectionBehaviorDto = JSON.parseObject(collectionString, CollectionBehaviorDto.class);
            if (collectionBehaviorDto != null && BehaviorConstants.COLLECTION.equals(collectionBehaviorDto.getOperation())){
                isCollection = true;
            }
        }


        //封装数据返回
        HashMap<String, Object> data = new HashMap<>();

        data.put("isLike",isLike);
        data.put("isUnlike",isUnlike);
        data.put("isCollection",isCollection);
        data.put("isFollow",isFollow);

        return ResponseResult.okResult(data);

        //---------------以下代码测试用的--------------
        //是否关注
        // isLike = cacheService.hExists(likeKey, dto.getArticleId().toString());
        // isUnlike = cacheService.hExists(unLikeKey, dto.getArticleId().toString());
        // isCollection = cacheService.hExists(collectionKey, dto.getArticleId().toString());
        // isFollow = cacheService.hExists(followKey, dto.getArticleId().toString());

       /* HashMap<String, Object> data = new HashMap<>();

        data.put("isLike",isLike);
        data.put("isUnlike",isUnlike);
        data.put("isCollection",isCollection);
        data.put("isFollow",isFollow);

        return ResponseResult.okResult(data);*/

    }
}
